package com.mipo;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;

public class Delete {
	
	public static void main(String[] args) throws Exception {
		/*连接到MongoDB服务 如果是远程连接可以替换localhost为服务器所在IP地址
		两个参数分别服务器地和端口*/
		ServerAddress serverAddress = new ServerAddress("172.31.12.54", 22017);
		List<ServerAddress> addrs = new ArrayList<ServerAddress>();
		addrs.add(serverAddress);
		//三个参数分别用户名、数据库名、密码
		MongoCredential credential = MongoCredential.createMongoCRCredential("zsmongo", "third_swap_data", "zsmd@017".toCharArray());
		List<MongoCredential> credentials = new ArrayList<MongoCredential>();
		credentials.add(credential);
		//通过连接认证获取MongoDB连接
		MongoClient mongoClient = new MongoClient(addrs, credentials);
		MongoDatabase db = mongoClient.getDatabase("third_swap_data");
		MongoCollection<Document> users = db.getCollection("c006");
		
		delete(users);
		
		mongoClient.close();
		
	}
	
    public static void delete(MongoCollection<Document> users){
    	DeleteResult result = users.deleteOne(new BasicDBObject("_id", new ObjectId("5b20ee98d896373074781180")));
    	System.out.println("9:" + result.getDeletedCount());
    }

}


